/*
  (C) 2005 Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package step;

import java.net.URI;
import java.util.ArrayList;

import step.exceptions.STEPException;
import http.*;

public class ReceiverGetSocketNode extends ReceiverSocketNode
{
	public ReceiverGetSocketNode(String id, String fromID, URI fromURI) {
		super(id, fromID, fromURI);
	}

	public ReceiverGetSocketNode(String fromID, URI fromURI) {
		super(fromID, fromURI);
	}

	public String GetInfoXML() { 
		return super.GetInfoXML("GET", "receiver", FromURI); 
	}
	
    Client getter;

    public boolean IsEnabled()
    {
	if (!rt_evaluated) {
	    if (getter!=null) {
		if (getter.Done())
		    return true;
		else
		    return false;
	    } else {
		return true;
	    }
	} else {
	    return false;
	}
    }

    protected void Evaluate() throws EvaluationFailure
    {
	if (getter==null) {
	    getter = http.Client.GET_nb(FromURI);
	} else {
	    assert(getter.Done());
	    SetValue(getter.Value());
	    PushResult();
	    getter = null;
	    rt_evaluated = true;
	}
    }


    public void Write(java.io.OutputStream out, int depth) throws java.io.IOException
    {
	WriteBasis(out, depth, "type=\"receiver\" method=\"get\"");
    }

	@Override
	public void validate(Graph g, ArrayList<STEPException> compilationErrors) {
		// TODO Auto-generated method stub
		
	}	
	
	@Override
	public Node ShallowCopy() {
		ReceiverGetSocketNode cn = new ReceiverGetSocketNode(ID, FromID, FromURI);
		Node.CopyNonRTPropertiesFromAtoB(this,cn);
		return cn;
	}
}

